﻿using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web;

namespace Lync.Widget.Library.ServiceHelpers
{
    public class ParameterParser : IParameterParser
    {
        public Dictionary<string,string> Parse(string query, char separator = '+')
        {
            var result = new Dictionary<string, string>();

            const string separatorReplacer = "*replacer*";

            query = query.Replace(separator.ToString(CultureInfo.InvariantCulture), separatorReplacer);

            var parameters = HttpUtility.ParseQueryString(query, Encoding.UTF8);

            foreach (string key in parameters.Keys)
            {
                var value = parameters[key];

                if (!string.IsNullOrEmpty(value))
                {
                    result.Add(key, value.Replace(separatorReplacer, separator.ToString(CultureInfo.InvariantCulture)));
                }
            }

            return result;
        }
    }
}